import { getAllBack,dealFeekBack ,getSearch} from "../../api/manage";
import { searchNews } from "../../api/news";

Page({
  data: {
    feedbackMessages: [],
    searchText: '',
    replyDialogVisible: false, // 控制回复框的显示
    currentReplyIndex: null, // 当前回复的消息索引
    statusOptions: ['已处理', '处理中', '未处理'], // 状态选项
    selectedStatus: '' ,// 当前选择的状态
    replyContent: '', // 用于保存回复内容


  },

 async onLoad() {
  await this.fetchAndFormatFeedbackMessages(1); // 传入所需的 num
  },
  async fetchAndFormatFeedbackMessages(num) {
    let res = await getAllBack();
    // 格式化反馈消息
    let formattedMessages = res.data.map(message => {
      if (message.createTime) {
        message.createTime = message.createTime.replace("T", " "); // 替换 "T" 为 " "
      }
      if (message.result === null) {
        message.result = "未处理";
      }
      return message;
    });
    
    this.setData({
      feedbackMessages: formattedMessages
    });
  },
  onSearchInput(e) {
    const searchText = e.detail.value;
    this.setData({ searchText });
  },
  // 处理点击搜索图标
  onSearchClick() {
    this.searchNews()
    
  },
  onSearchConfirm() {
    this.searchNews();
  },
  async searchNews(){
    if (this.data.searchText) {
      // 发送搜索请求
      const res=await getSearch(this.data.searchText);
      if(res.code===200) {
        if(res.data.length===0) {
          wx.showToast({
            title: '暂无相关内容',
            icon: 'none',
          });
          return ;
        }
        this.setData({
          feedbackMessages: res.data 
        });      }
    } else {
      await this.fetchAndFormatFeedbackMessages(1); // 传入所需的 num

    }
  },
  formatDate(dateString) {
    const date = new Date(dateString);
    const year = date.getFullYear();
    const month = date.getMonth() + 1;
    const day = date.getDate();
    const hours = date.getHours();
    const minutes = date.getMinutes();
    return `${year}年${month}月${day}日 ${hours}时${minutes}分`;
  },

  onReplyClick(e) {
    const index = e.currentTarget.dataset.index;
    console.log('inde',e.currentTarget.dataset.index);
    this.setData({
      currentReplyIndex: index,
      replyDialogVisible: true,
    });
  },

  async onConfirmReply() {
    // 处理回复逻辑，比如发送回复到服务器
    console.log('回复内容',this.data.replyContent);
     // 检查回复内容是否为空
  if (this.data.replyContent.trim() === '') {
    wx.showToast({
      title: '回复内容不能为空',
      icon: 'none',
    });
    return;
  }
  // 准备发送的数据
  const requestData = {
    feedbackId: this.data.currentReplyIndex,
    result: this.data.replyContent.trim() !== '' ? '已处理' : '', // 如果有回复内容，则设为 "处理中"
    response: this.data.replyContent
  };
  let res = await dealFeekBack(requestData);
    this.setData({ replyDialogVisible: false });
    if(res.code===200) {
      wx.showToast({
        title: '回复成功',
        icon: 'success',
      });
      await this.fetchAndFormatFeedbackMessages(1); 

    }
  
  },

  onCancelReply() {
    this.setData({ replyDialogVisible: false });
  },
  // 选择状态的处理函数
  onStatusChange(e) {
    const index = e.detail.value;
    this.setData({
      selectedStatus: this.data.statusOptions[index]
    });
  },
  onReplyInput(e) {
    this.setData({
      replyContent: e.detail.value 
    });
    console.log('ee',e.detail.value );
  },
});
